using System;
using System.Dynamic;

namespace Plain.Search{
	public partial class PropertyFactory<T>:DynamicObject{
		public PropertyFactory (){
		}
		
		public PropertyIdentifier this[string s]{
			get{
				if(s.Contains(".")){
					var ps = s.Split('.');
					Type t = typeof(T);
					foreach(string p in ps){
						var v =t.GetProperty(p);
						if(v==null){
							throw new Exception(s + " is not a valid property for " + typeof(T).Name);
						}else{
							t=v.PropertyType;
						}
					}
					return new PropertyIdentifierGeneric(s,t);
					
				}
				else if(typeof(T).GetProperty(s) !=null){
					return new PropertyIdentifierGeneric(s,typeof(T).GetProperty(s).PropertyType);
				}else{
					throw new Exception(s + " is not a valid property for " + typeof(T).Name);
				}
			}
		}
		public static ISearchResult<T> operator / (PropertyFactory<T> fact , Criteria crit){
			return new SearchResult<T>(crit);
		}
		public static Join<T> operator * (PropertyFactory<T> fact , PropertyIdentifier prop){
			var j = new Join<T>();
			j.LeftJoin(prop);
			return j;
		}
		public static T operator * (PropertyFactory<T> fact, object o){
			return default(T);
		}
        public override bool TryGetMember(GetMemberBinder binder, out object result) {
            result = this[binder.Name];
            return true;
        }
	}
}

